const fsPromises = require('fs').promises;
const path = require('path');

class GenerateTSModal {
    constructor(url) {
        this.baseUrl = path.resolve(process.cwd(), url);
    }
    createH5Model(url, fileName) {
        this.writeModelContent(url, `H5_${fileName}`, json => (a, b) => {
            const fontClass = b.font_class.replace(/-/g, '_');
            return a += `\n\t${`${fontClass.slice(0, 1).toUpperCase()}${fontClass.slice(1)}`} = 'iconfont ${json[ 'css_prefix_text' ]}${fontClass}',`;
        });
    }
    createRNModel(url, fileName) {
        this.writeModelContent(url, `RN_${fileName}`, () => (a, b) => {
            const fontClass = b.font_class.replace(/-/g, '_');
            return a += `\n\t${`${fontClass.slice(0, 1).toUpperCase()}${fontClass.slice(1)}`} = '0x${b.unicode}',`;
        });
    }
    writeModelContent(url, name, callback) {
        const res = require(path.join(this.baseUrl, url));
        let str = `enum ${name} {`;
        str = res.glyphs.reduce(callback(res), str);
        str += '\n}\n';
        str += `export default ${name};`;
        fsPromises.writeFile(path.join(this.baseUrl, `@${name}.ts`), str);
    }
}
module.exports = GenerateTSModal;